package vashion.azeroth.core.manager.item;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import vashion.azeroth.common.math.BigDecimalUtil;
import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.ItemErrorEnum;
import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.core.item.pojo.ForestDO;
import vashion.azeroth.core.item.pojo.ItemDO;
import vashion.azeroth.core.item.pojo.ItemSkuDO;
import vashion.azeroth.core.item.pojo.PropertyValueDO;
import vashion.azeroth.core.item.query.ItemQueryDO;
import vashion.azeroth.core.manager.forest.BaseForestManager;
import vashion.azeroth.core.manager.forest.BasePropertyValueManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.manager.personal.config.ConfigExchangeRateManager;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;

import com.google.common.collect.Lists;

/**
 * Created by tingting on 15/8/31. 商品读接口
 */
@Service
public class ItemReadManger {

	private final static Logger log = LoggerFactory.getLogger(ItemReadManger.class);

	@Autowired
	private BaseItemManager baseItemManager;
	@Autowired
	private BaseItemSkuManager baseItemSkuManager;
	@Autowired
	private BasePropertyValueManager basePropertyValueManager;
	@Autowired
	private BaseForestManager baseForestManager;
	@Autowired
	private PersonalMemberManager memberManager;
	@Autowired
	private ConfigExchangeRateManager configExchangeRateManager;

	/**
	 * 根据商品id获取商品详情
	 *
	 * @param itemId
	 * @param needSku
	 * @return
	 */
	public ApiResult<ItemDO> getItemDetail(long itemId, boolean needSku) {

		ItemDO itemDO = null;
		try {
			itemDO = baseItemManager.get(itemId);
			ForestDO forestDO = baseForestManager.getWithCache(itemDO.getCategoryId());
			if (null != forestDO) {
				itemDO.setTariff(forestDO.getTariff());
				//设置商品的完整类目名字
				itemDO.setCategoryFullTitle(getForestFullTitleByForest(forestDO).replace("-", " "));
			}
			if (needSku) {
				ItemSkuDO skuDO = new ItemSkuDO();
				skuDO.setItemId(itemId);
				skuDO.setPageSize(AzerothConstants.Item.MAX_SKU_COUNT);
				List<ItemSkuDO> itemSkuDOList = baseItemSkuManager.find(skuDO);
				itemDO.setItemSkuDOList(itemSkuDOList);
				// 解析商品的销售属性
				ApiResult<List<PropertyValueDO>> valueDOList = basePropertyValueManager
						.getValueList(Lists.newArrayList(itemDO.getFeatureIdList()));
				if (valueDOList.isSuccess()) {
					log.info("item have not orther property itemId=" + itemId);
					itemDO.setFeatureValueDOList(valueDOList.getData());
				}
			}
		} catch (Exception e) {
			log.error("baseItemManager.get error itemId=" + itemId, e);
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_QUERY_ERROR.getErrorCode(),
					ItemErrorEnum.ITEM_QUERY_ERROR.getErrorMessage());
		}
		return ApiResult.newSuccessResult(itemDO);
	}
	
	/**
	 * 根据商品id获取商品详情
	 *
	 * @param itemId
	 * @return
	 */
	public ApiResult<ItemDO> getItemDetailforBackAdmin(long itemId) {
		ApiResult<ItemDO> itemResult = this.getItemDetail(itemId, true);
		if (itemResult.isSuccess() && null != itemResult.getData()) {
			ItemDO item = itemResult.getData();
			// 设置类目全名，用于前台显示
			setForestFullTitle(item);
		}
		return itemResult;
	}
	/**
	 * 搜索商品，目前支持字段：userId,title,categoryId,gmtCreate
	 *
	 * @param itemDO
	 * @return
	 */
	public ApiResult<List<ItemDO>> searchItem(ItemDO itemDO) {
		List<ItemDO> itemList = null;
		try {
			itemList = baseItemManager.find(itemDO);
		} catch (Exception e) {
			log.error("baseItemManager.find error, itemDO=" + itemDO);
			return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return ApiResult.newSuccessResult(itemList);
	}

	/**
	 * 搜索商品
	 *
	 * @param itemQueryDO
	 * @return
	 */
	public ApiResult<List<ItemDO>> queryItem(ItemQueryDO itemQueryDO) {

		if (null == itemQueryDO) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_ARG_ERROR.getErrorCode(),
					ItemErrorEnum.ITEM_ARG_ERROR.getErrorMessage());
		}
		if (itemQueryDO.getPageNO() > 0 && itemQueryDO.getStart() <= 0) {
			itemQueryDO.setStart((itemQueryDO.getPageNO() - 1) * itemQueryDO.getPageSize());
		}
		if (!StringUtils.isBlank(itemQueryDO.getBrandTitle())) {
			// 根据品牌名称查询品牌Id
			PropertyValueDO propertyValueDO = new PropertyValueDO();
			propertyValueDO.setValue(itemQueryDO.getBrandTitle());
			List<PropertyValueDO> propertyValueDOList = basePropertyValueManager.find(propertyValueDO);
			if (!CollectionUtils.isEmpty(propertyValueDOList)) {
				itemQueryDO.setBrandId(propertyValueDOList.get(0).getId());
			} else {
				return ApiResult.newErrorResult(ItemErrorEnum.ITEM_ARG_ERROR.getErrorCode(),
						"没有对应的品牌:" + itemQueryDO.getBrandTitle());
			}
		}

		List<ItemDO> itemList = null;
		try {
			itemList = baseItemManager.queryItem(itemQueryDO);
		} catch (Exception e) {
			log.error("baseItemManager.queryItem error, itemQueryDO=" + itemQueryDO, e);
			return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		ApiResult<List<ItemDO>> apiResult = ApiResult.newSuccessResult(itemList);
		// 为分页功能添加商品的总数量
		ApiResult<Integer> countResult = this.countQueryItem(itemQueryDO);
		if (countResult.isSuccess()) {
			apiResult.setTotalCount(countResult.getData());
		}
		return apiResult;
	}

	/**
	 * 搜索商品，count接口
	 *
	 * @param itemQueryDO
	 * @return
	 */
	public ApiResult<Integer> countQueryItem(ItemQueryDO itemQueryDO) {
		Integer count = null;
		try {
			count = baseItemManager.countQueryItem(itemQueryDO);
		} catch (Exception e) {
			log.error("baseItemManager.countQueryItem error, itemQueryDO=" + itemQueryDO);
			return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return ApiResult.newSuccessResult(count);
	}

	/**
	 * @param itemQueryDO
	 * @param brandIdListString
	 * @param categoryIdListString
	 * @return 符合查询条件商品的数量
	 */
	public ApiResult<Integer> countQueryItemForBackAdmin(ItemQueryDO itemQueryDO, String brandIdListString,
			String categoryIdListString) {
		// 拆分brandIdList
		try {
			List<Long> brandIdList = idListString2IdList(brandIdListString);
			if (!CollectionUtils.isEmpty(brandIdList)) {
				itemQueryDO.setBrandIdList(brandIdList);
			}
		} catch (NumberFormatException e) {
			return ApiResult.newErrorResult(ItemErrorEnum.BRAND_ID_ERROR.getErrorCode(),
					ItemErrorEnum.BRAND_ID_ERROR.getErrorMessage());
		}

		// 拆分categoryIdList
		try {
			List<Long> categoryIdList = idListString2IdList(categoryIdListString);
			if (!CollectionUtils.isEmpty(categoryIdList)) {
				itemQueryDO.setCategoryIdList(categoryIdList);
			}
		} catch (NumberFormatException e) {
			return ApiResult.newErrorResult(ItemErrorEnum.CATEGORY_ID_ERROR.getErrorCode(),
					ItemErrorEnum.CATEGORY_ID_ERROR.getErrorMessage());
		}

		Integer count = null;
		try {
			count = baseItemManager.countQueryItem(itemQueryDO);
		} catch (Exception e) {
			log.error("baseItemManager.countQueryItem error, itemQueryDO=" + itemQueryDO);
			return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return ApiResult.newSuccessResult(count);
	}

	public ApiResult<List<String>> getRecommendTitle(String word) {

		try {
			List<String> titleList = baseItemManager.getRecommendTitle(word);
			return ApiResult.newSuccessResult(titleList);
		} catch (Exception e) {
			log.error("baseItemManager.getRecommendTitle error word=" + word, e);
			return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 供应商搜索商品，只显示自己发布的商品
	 *
	 * @param member
	 * @param itemQueryDO
	 * @return
	 */
	private ApiResult<List<ItemDO>> getItemListforSupplier(PersonalMember member, ItemQueryDO itemQueryDO) {
		itemQueryDO.setUserId(member.getMemberId().longValue());
		return queryItem(itemQueryDO);
	}

	/**
	 * 代理商搜索商品，可以选择只显示自己的供应商商品
	 *
	 * @param member
	 * @param itemQueryDO
	 * @param isMyInvitor
	 *            是否只显示自己供应商的商品
	 * @param targetCurno
	 * @return
	 */
	private ApiResult<List<ItemDO>> getItemListforAgent(PersonalMember member, ItemQueryDO itemQueryDO,
			boolean isMyInvitor, String targetCurno) {
		ApiResult<List<ItemDO>> itemList;
		if (isMyInvitor) {
			itemQueryDO.setUserId(member.getInviteMemberId().longValue());
		}
		itemQueryDO.setStatus(AzerothConstants.Item.NORMAL);
		// 查询价格换算成人民币
		if (StringUtils.isEmpty(targetCurno)) {
			targetCurno = "CNY";
		}

		if (null != itemQueryDO.getPriceStart()) {
			itemQueryDO.setPriceStart(exchangePrice(targetCurno, null, itemQueryDO.getPriceStart()));
		}
		if (null != itemQueryDO.getPriceEnd()) {
			itemQueryDO.setPriceEnd(exchangePrice(targetCurno, null, itemQueryDO.getPriceEnd()));
		}
		// 查询商品
		itemList = queryItem(itemQueryDO);
		// 查询失败则返回
		if (!itemList.isSuccess() || null == itemList.getData()) {
			return itemList;
		}
		for (ItemDO itemDO : itemList.getData()) {
			if (!StringUtils.isEmpty(itemDO.getItemCode())) {
				// 设置库存为同商品编码商品的总库存
				Integer totalStock = 0;
				ApiResult<List<ItemDO>> itemListResult = getItemListByItemCode(itemDO.getItemCode(),
						AzerothConstants.Item.NORMAL);
				if (null != itemListResult && !CollectionUtils.isEmpty(itemListResult.getData())) {
					for (ItemDO itemSameCode : itemListResult.getData()) {
						totalStock += itemSameCode.getStock();
					}
					itemDO.setStock(totalStock);
				}
				// 抹平差价
				if (!isMyInvitor && member.getInviteMemberId() != itemDO.getUserId()) {
					itemDO.setPrice(getHigherSupplierPrice(member, itemDO));
				}
				// 转化为指定货币的价格
				exchangePrice(itemDO, targetCurno);
			}
		}
		return itemList;
	}

	/**
	 * 代理商和供应商搜索商品
	 *
	 * @param member
	 * @param itemQueryDO
	 * @param isMyInvitor
	 * @param targetCurno
	 * @return
	 */
	public ApiResult<List<ItemDO>> getItemListforSupplierAndAgent(PersonalMember member, ItemQueryDO itemQueryDO,
			boolean isMyInvitor, String brandIdListString, String categoryIdListString, String targetCurno) {
		// 拆分brandIdList
		try {
			List<Long> brandIdList = idListString2IdList(brandIdListString);
			if (!CollectionUtils.isEmpty(brandIdList)) {
				itemQueryDO.setBrandIdList(brandIdList);
			}
		} catch (NumberFormatException e) {
			return ApiResult.newErrorResult(ItemErrorEnum.BRAND_ID_ERROR.getErrorCode(),
					ItemErrorEnum.BRAND_ID_ERROR.getErrorMessage());
		}

		// 拆分categoryIdList
		try {
			List<Long> categoryIdList = idListString2IdList(categoryIdListString);
			List<Long> categoryIdListQuery = Lists.<Long>newArrayList();
			if (!CollectionUtils.isEmpty(categoryIdList)) {
				categoryIdListQuery.addAll(categoryIdList);
				// 查询子类目id，并加入查询类目id列表
				for (Long categoryIdTemp : categoryIdList) {
					ApiResult<List<ForestDO>> childrenResult = baseForestManager.getChildrenForestList(categoryIdTemp);
					if (childrenResult.isSuccess() && !CollectionUtils.isEmpty(childrenResult.getData())) {
						for (ForestDO childForestTemp : childrenResult.getData()) {
							categoryIdListQuery.add(childForestTemp.getId());
						}
					}
				}
				itemQueryDO.setCategoryIdList(categoryIdListQuery);
			}
		} catch (NumberFormatException e) {
			return ApiResult.newErrorResult(ItemErrorEnum.CATEGORY_ID_ERROR.getErrorCode(),
					ItemErrorEnum.CATEGORY_ID_ERROR.getErrorMessage());
		}

		ApiResult<List<ItemDO>> itemList;
		// 检查用户是供应商还是代理商
		member = memberManager.get(member.getMemberId());
		if (null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		} else if (AzerothConstants.PersonalMember.MemberType.supplier == member.getType()) {
			itemList = getItemListforSupplier(member, itemQueryDO);
		} else if (AzerothConstants.PersonalMember.MemberType.agent == member.getType()) {
			itemList = getItemListforAgent(member, itemQueryDO, isMyInvitor, targetCurno);
		} else {
			return ApiResult.newErrorResult(MemberErrorEnum.NOT_EXISTID.getErrorCode(),
					MemberErrorEnum.NOT_EXISTID.getErrorMessage());
		}
		return itemList;
	}

	/**
	 * 管理平台搜索商品
	 *
	 * @param itemQueryDO
	 * @param brandIdListString
	 * @param categoryIdListString
	 * @return
	 */
	public ApiResult<List<ItemDO>> getItemListforBackAdmin(ItemQueryDO itemQueryDO, String brandIdListString,
			String categoryIdListString) {
		// 拆分brandIdList
		try {
			List<Long> brandIdList = idListString2IdList(brandIdListString);
			if (!CollectionUtils.isEmpty(brandIdList)) {
				itemQueryDO.setBrandIdList(brandIdList);
			}
		} catch (NumberFormatException e) {
			return ApiResult.newErrorResult(ItemErrorEnum.BRAND_ID_ERROR.getErrorCode(),
					ItemErrorEnum.BRAND_ID_ERROR.getErrorMessage());
		}

		// 拆分categoryIdList
		try {
			List<Long> categoryIdList = idListString2IdList(categoryIdListString);
			if (!CollectionUtils.isEmpty(categoryIdList)) {
				itemQueryDO.setCategoryIdList(categoryIdList);
			}
		} catch (NumberFormatException e) {
			return ApiResult.newErrorResult(ItemErrorEnum.CATEGORY_ID_ERROR.getErrorCode(),
					ItemErrorEnum.CATEGORY_ID_ERROR.getErrorMessage());
		}
		ApiResult<List<ItemDO>> itemListResult = this.queryItem(itemQueryDO);
		// 设置类目全名，用于前台显示
		if (itemListResult.isSuccess() && null != itemListResult.getData()) {
			for (ItemDO itemTemp : itemListResult.getData()) {
				setForestFullTitle(itemTemp);
			}
		}
		return itemListResult;
	}

	public ApiResult<List<ItemDO>> getSameCodeItemListForAgent(PersonalMember member, ItemQueryDO itemQueryDO,
			String targetCurno) {
		// 检查用户是供应商还是代理商
		member = memberManager.get(member.getMemberId());
		if (null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		} else if (AzerothConstants.PersonalMember.MemberType.supplier == member.getType()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		} else if (AzerothConstants.PersonalMember.MemberType.agent == member.getType()
				&& null != itemQueryDO.getItemId()) {
			ItemDO itemDO = baseItemManager.get(itemQueryDO.getItemId());
			if (!StringUtils.isEmpty(itemDO.getItemCode())) {
				// 查询同商品编码商品
				ApiResult<List<ItemDO>> itemListResult = getItemListByItemCode(itemDO.getItemCode(),
						AzerothConstants.Item.NORMAL);
				if (null != itemListResult && !CollectionUtils.isEmpty(itemListResult.getData())) {
					// 移除输入商品
					ItemDO sameItem = null;
					for (ItemDO itemSameCode : itemListResult.getData()) {
						if (itemDO.getId() == itemSameCode.getId()) {
							sameItem = itemSameCode;
						}
						if (!StringUtils.isEmpty(itemSameCode.getItemCode())) {

							// 抹平差价
							if (member.getInviteMemberId() != itemSameCode.getUserId()) {
								itemSameCode.setPrice(getHigherSupplierPrice(member, itemSameCode));
							}
							// 转化为指定货币的价格
							exchangePrice(itemSameCode, targetCurno);
						}
					}
					if (null != sameItem)
						itemListResult.getData().remove(sameItem);
				}
				return itemListResult;
			} else {
				return ApiResult.newErrorResult(GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
						GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
			}
		} else {
			return ApiResult.newErrorResult(MemberErrorEnum.NOT_EXISTID.getErrorCode(),
					MemberErrorEnum.NOT_EXISTID.getErrorMessage());
		}
	}

	/**
	 * 供应商搜索商品详情
	 *
	 * @param member
	 * @param itemId
	 * @param needSku
	 * @return
	 */
	private ApiResult<ItemDO> getItemDetailforSupplier(PersonalMember member, long itemId, boolean needSku) {
		ApiResult<ItemDO> itemDOResult = getItemDetail(itemId, needSku);
		if (member.getMemberId() != itemDOResult.getData().getUserId()) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_NOT_OWER.getErrorCode(),
					ItemErrorEnum.ITEM_NOT_OWER.getErrorMessage());
		}
		return itemDOResult;
	}

	/**
	 * 代理商搜索商品详情
	 *
	 * @param member
	 * @param itemId
	 * @param needSku
	 * @param targetCurno
	 * @return
	 */
	private ApiResult<ItemDO> getItemDetailforAgent(PersonalMember member, long itemId, boolean needSku,
			String targetCurno) {
		ApiResult<ItemDO> itemDOResult = getItemDetail(itemId, needSku);
		ItemDO itemDO = itemDOResult.getData();
		// 抹平差价
		if (member.getInviteMemberId() != itemDO.getUserId() && !StringUtils.isEmpty(itemDO.getItemCode())) {
			itemDO.setPrice(getHigherSupplierPrice(member, itemDO));
			;
		}
		exchangePrice(itemDO, targetCurno);
		return itemDOResult;
	}

	/**
	 * 代理商和供应商搜索商品详情
	 *
	 * @param member
	 * @param itemId
	 * @param needSku
	 * @param targetCurno
	 * @return
	 */
	public ApiResult<ItemDO> getItemDetailforSupplierAndAgent(PersonalMember member, long itemId, boolean needSku,
			String targetCcurno) {
		ApiResult<ItemDO> itemDOResult;
		// 检查用户是供应商还是代理商
		member = memberManager.get(member.getMemberId());
		if (null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		} else if (AzerothConstants.PersonalMember.MemberType.supplier == member.getType()) {
			itemDOResult = getItemDetailforSupplier(member, itemId, needSku);
		} else if (AzerothConstants.PersonalMember.MemberType.agent == member.getType()) {
			itemDOResult = getItemDetailforAgent(member, itemId, needSku, targetCcurno);
		} else {
			return ApiResult.newErrorResult(MemberErrorEnum.NOT_EXISTID.getErrorCode(),
					MemberErrorEnum.NOT_EXISTID.getErrorMessage());
		}
		return itemDOResult;
	}

	/**
	 * 非关联供应商的商品，如果低于关联供应商商品价格，设定为供应商价格。并且无论如何，一定会有1%的关联供应商提成
	 * 
	 * @param member
	 * @param itemDO
	 */
	public long getHigherSupplierPrice(PersonalMember member, ItemDO itemDO) {
		ItemQueryDO itemDoByItemCode = new ItemQueryDO();
		itemDoByItemCode.setItemCode(itemDO.getItemCode());
		itemDoByItemCode.setUserId(member.getInviteMemberId());
		itemDoByItemCode.setStatus(AzerothConstants.Item.NORMAL);
		long higherPrice = itemDO.getPrice(), originPrice = itemDO.getPrice();
		// 寻找关联供应商的相同商品
		List<ItemDO> itemListResult = baseItemManager.querySameCodeItem(itemDoByItemCode);
		// 如果比关联供应商价格低，设定为关联供应商的价格
		if (null != itemListResult && !CollectionUtils.isEmpty(itemListResult)) {
			for (ItemDO itemSameCode : itemListResult) {
				if (higherPrice < itemSameCode.getPrice()) {
					higherPrice = itemSameCode.getPrice();
				}
			}
		}
		// 加上1%的关联供应商提成
		higherPrice = BigDecimalUtil
				.initAdd(BigDecimal.valueOf(higherPrice),
						BigDecimalUtil.initMultiply(Long.toString(originPrice),
								AzerothConstants.Item.PRICE_ENHANCE.toString()))
				.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
		return higherPrice;
	}

	/**
	 * 换算成目标币种的价格
	 * 
	 * @param itemDO
	 * @param targetCurno
	 * @return
	 */
	public ItemDO exchangePrice(ItemDO itemDO, String targetCurno) {
		if (StringUtils.isEmpty(targetCurno)) {
			targetCurno = "CNY";
		}

		if (targetCurno.equalsIgnoreCase(itemDO.getPriceCurno())) {
			return itemDO;
		}
		long price = itemDO.getPrice();
		price = exchangePrice(itemDO.getPriceCurno(), targetCurno, price);
		itemDO.setPriceCurno(targetCurno);
		itemDO.setPrice(price);
		return itemDO;
	}

	/**
	 * 换算成目标币种的价格
	 * 
	 * @param fromCurno
	 * @param targetCurno
	 * @param price
	 * @return
	 */
	public long exchangePrice(String fromCurno, String targetCurno, long price) {
		if (StringUtils.isEmpty(fromCurno)) {
			fromCurno = "CNY";
		}
		if (StringUtils.isEmpty(targetCurno)) {
			targetCurno = "CNY";
		}
		// 转化成人民币
		if (!"CNY".equalsIgnoreCase(fromCurno)) {
			price = configExchangeRateManager.transferToCNY(fromCurno, price);
		}
		// 转化成目标货币
		if (StringUtils.isNotEmpty(targetCurno) && !"CNY".equalsIgnoreCase(targetCurno)) {
			price = configExchangeRateManager.transferFromCNY(targetCurno, price);
		}
		return price;
	}

	/**
	 * 根据itemCode获取指定状态的商品列表
	 * 
	 * @param itemCode
	 * @param status
	 * @return 指定的itemCode和状态的商品列表
	 */
	private ApiResult<List<ItemDO>> getItemListByItemCode(String itemCode, int status) {
		if (StringUtils.isEmpty(itemCode))
			return null;
		ItemQueryDO itemDoByItemCode = new ItemQueryDO();
		itemDoByItemCode.setItemCode(itemCode);
		itemDoByItemCode.setStatus(status);
		itemDoByItemCode.setPageSize(1000);

		List<ItemDO> itemList = baseItemManager.querySameCodeItem(itemDoByItemCode);
		ApiResult<List<ItemDO>> itemListResult = ApiResult.newSuccessResult(itemList);

		return itemListResult;
	}

	/**
	 * @param idListString
	 * @throws NumberFormatException
	 */
	private List<Long> idListString2IdList(String idListString) throws NumberFormatException {
		if (StringUtils.isNotEmpty(idListString)) {
			String[] idArray = idListString.split(",");
			List<Long> idList = new ArrayList<Long>();
			for (String id : idArray) {
				Long listlong = Long.parseLong(id);
				idList.add(listlong);
			}
			if (!idList.isEmpty()) {
				return idList;
			}
		}
		return null;
	}

	/**
	 * @param itemDO
	 */
	private void setForestFullTitle(ItemDO itemDO) {
		ForestDO forest = baseForestManager.get(itemDO.getCategoryId());
		if (null != forest) {
			String forestFullTitle = getForestFullTitleByForest(forest);
			itemDO.setCategoryFullTitle(forestFullTitle);
		}
	}

	/**
	 * @param forest
	 * @return 包括父类目名字的完整的类目名字
	 */
	private String getForestFullTitleByForest(ForestDO forest) {
		String forestFullTitle = forest.getTitle();
		Long parentId = forest.getParentId();
		if (null != parentId && parentId > 0) {
			ForestDO parentForest = baseForestManager.get(parentId);
			if (null != parentForest && null != parentForest.getTitle()) {
				forestFullTitle = parentForest.getTitle() + "-" + forest.getTitle();
			}
		}
		return forestFullTitle;
	}

}
